<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>login</title>
    <!-- 最新版本的 Bootstrap 核心 CSS 文件 -->
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css"
          integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
    <!-- jQuery (Bootstrap 的所有 JavaScript 插件都依赖 jQuery，所以必须放在前边) -->
    <script src="https://cdn.jsdelivr.net/npm/jquery@1.12.4/dist/jquery.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/js/bootstrap.min.js"
            integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa"
            crossorigin="anonymous"></script>
    <!-- jquery-validator -->
    <script src="https://cdn.bootcss.com/jquery-validate/1.19.1/jquery.validate.min.js"></script>
    <script src="https://cdn.bootcss.com/jquery-validate/1.19.1/localization/messages_zh.js"></script>
    <!--    layer-->
    <script src="https://cdn.bootcss.com/layer/1.8.5/layer.min.js"></script>
    <!-- md5.js -->
    <!--    <script src="https://github.com/emn178/js-md5/blob/master/build/md5.min.js"></script>-->
    <script type="text/javascript" th:src="@{/js/md5.min.js}"></script>
    <!-- common.js -->
    <script type="text/javascript" th:src="@{/js/common.js}"></script>
</head>
<body>
<form class="form-horizontal" name="loginForm" id="loginForm" method="post" style="width:50%; margin:0 auto">
    <h2 style="text-align:center; margin-bottom: 20px">user login</h2>
    <div class="form-group">
        <label for="phoneNumber" class="col-sm-2 control-label">Cell Phone</label>
        <div class="col-sm-10">
            <input type="text" class="form-control" name="phoneNumber" id="phoneNumber" placeholder="Cell phone 11"
                   required="true"
                   minlength="11" maxlength="11">
        </div>
    </div>
    <div class="form-group">
        <label for="inputPassword" class="col-sm-2 control-label">Password</label>
        <div class="col-sm-10">
            <input type="password" class="form-control" id="inputPassword" placeholder="Password 6-16"
                   required="required"
                   minlength="6" maxlength="16">
        </div>
    </div>
    <div class="form-group">
        <div class="col-sm-offset-2 col-sm-10">
            <button type="submit" class="btn btn-default" onclick="login()">Sign in</button>
        </div>
    </div>
</form>
</body>
<script>
    function login() {
        $("#loginForm").validate({
            submitHandler: function (form) {
                doLogin();
            }
        });
    }

    function doLogin() {
        g_showLoading();
        var inputPass = $("#inputPassword").val();
        var salt = g_passsword_salt;
        var str = "" + salt[0] + salt[2] + inputPass + salt[5] + salt[4];
        var password = md5(str);

        $.post({
            url: "/login/do_login",
            data: {
                phoneNumber: $("#phoneNumber").val(),
                password: password
            },
            success: function (data) {
                layer.closeAll();
                if (0 === data.code) {
                    layer.msg("sign in success");
                    window.location.href = ("/goods/to_list");
                } else {
                    layer.msg(data.msg);
                }
            },
            error: function () {
                layer.closeAll();
            }
        })

    }


</script>
</html>